<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\dao\car;


use app\dao\BaseDao;
use app\model\car\CarOrderRefund;

class CarOrderRefundDao extends BaseDao
{
    protected function setModel(): string
    {
        return CarOrderRefund::class;
    }

    /**
     * 列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public function getList(array $where, int $page, int $limit){
        $model = $this->getModel()->alias('r')->where('r.is_del',0)->where('r.order_type','=',$where['order_type']);
        if($where['order_type'] == 1){
            $model->join('car_order o','o.id=r.order_id','left')->join('user u','u.uid=r.uid','left');
            $model->field('r.*,o.order_no,o.price as order_price,u.nickname,o.pay_time,o.pay_type');
        }else{
            $model->join('car_order_rerent t','t.id=r.order_id','left')->join('user u','u.uid=r.uid','left')->join('car_order o','o.id=t.order_id','left');
            $model->field('r.*,t.rerent_no as order_no,t.rerent_price as order_price,u.nickname,t.pay_time,t.pay_type');
        }
        $model = $this->setWheres($model,$where);
        $count = $model->count();
        $list = $model->page($page,$limit)->order('id desc')->select()->toArray();
        return compact('list','count');
    }

    /**
     * 条件
     * @param $model
     * @param $where
     * @return mixed
     */
    public function setWheres($model,$where){
        if($where['order_type'] == 1){
            if(isset($where['keyword']) && $where['keyword']){
                $model->where('o.order_no|r.refund_no','like',"%{$where['keyword']}%");
            }
        }else{
            if(isset($where['keyword']) && $where['keyword']){
                $model->where('t.rerent_no|r.refund_no','like',"%{$where['keyword']}%");
            }
        }
        if(isset($where['status']) && $where['status']){
            $model->where('r.status','=',$where['status']);
        }
        if(isset($where['place_id']) && $where['place_id']){
            $model->where('o.store_id','=',$where['place_id']);
        }
        return $model;
    }
}
